                include Common.asm

; ==========================================================
DataSeg         segment para public 'data'

        BtnSimplePlay   NearPtr 0
        BtnNormalPlay   NearPtr 0
        BtnOption       NearPtr 0
        BtnRanking      NearPtr 0
        BtnQuit         NearPtr 0

	TxtMainMenu	db "Main Menu", 0
        TxtSimplePlay   db "Simple Play",0
        TxtNormalPlay   db "Normal Play",0
        TxtOption       db "Music Settings",0
        TxtRanking      db "High Score",0
        TxtQuit         db "Quit",0

	TxtDoYouWantTo	db "Do you want to", 0
	TxtTurnOnMusic	db "turn on music?", 0

	TxtTheRecord	db "The Fastest Record is", 0
	TxtSeconds	db "Seconds", 0
	TxtNoRecord	db "No High Score", 0

        MenuStageClass  TWndClass { MenuStageOnInit, MenuStageOnCleanUp,
                MenuStageOnDraw, MenuStageOnProcess,
                MenuStageOnKey, MenuStageOnMouse, MenuStageOnCommand }

DataSeg         ends

; ==========================================================
CodeSeg         segment para public 'code'
        assume  ds: DataSeg

; ----------------------------------------------------------
; MenuStageOnInit - MenuStage OnInit Handler
; ----------------------------------------------------------
; Input:
;       si      Pointer to WndData
; Output:       none
; ----------------------------------------------------------
MenuStageOnInit proc near

	push	ax
	push    bx
	push    dx
	push    si
	push    di

	mov	bx, si
	mov     [bx].TWndData.SizeX, 180
	mov     [bx].TWndData.SizeY, 180
	push	si
	mov     si, offset TxtMainMenu
	lea     di, [bx].TWndData.Text
	call	StrCopy
	pop	si

	mov     [si].TWndData.Color, ColorDarkGray
	mov     dx, si

	; BtnSimplePlay
	call    AllocWndData
	mov     [bx].TWndData.WndClass, offset ButtonClass
	mov     [bx].TWndData.X, 30
	mov     [bx].TWndData.Y, 30
	mov     [bx].TWndData.SizeX, 120
	mov     [bx].TWndData.SizeY, 20
	mov     si, offset TxtSimplePlay
	lea     di, [bx].TWndData.Text
	call	StrCopy
	mov	si, bx
	mov     di, dx
	call    AddChildWnd
        mov     BtnSimplePlay, bx

        ; BtnNormalPlay
	call    AllocWndData
	mov     [bx].TWndData.WndClass, offset ButtonClass
	mov     [bx].TWndData.X, 30
	mov     [bx].TWndData.Y, 60
	mov     [bx].TWndData.SizeX, 120
	mov     [bx].TWndData.SizeY, 20
	mov     si, offset TxtNormalPlay
	lea     di, [bx].TWndData.Text
	call	StrCopy
	mov	si, bx
	mov     di, dx
	call    AddChildWnd
	mov     BtnNormalPlay, bx

	; BtnOption
	call    AllocWndData
	mov     [bx].TWndData.WndClass, offset ButtonClass
	mov     [bx].TWndData.X, 30
	mov     [bx].TWndData.Y, 90
	mov     [bx].TWndData.SizeX, 120
	mov     [bx].TWndData.SizeY, 20
	mov     si, offset TxtOption
	lea     di, [bx].TWndData.Text
	call	StrCopy
	mov	si, bx
	mov     di, dx
	call    AddChildWnd
	mov     BtnOption, bx

	; BtnRanking
	call    AllocWndData
	mov     [bx].TWndData.WndClass, offset ButtonClass
	mov     [bx].TWndData.X, 30
	mov     [bx].TWndData.Y, 120
	mov     [bx].TWndData.SizeX, 120
	mov     [bx].TWndData.SizeY, 20
	mov     si, offset TxtRanking
	lea     di, [bx].TWndData.Text
	call	StrCopy
	mov	si, bx
	mov     di, dx
	call    AddChildWnd
	mov     BtnRanking, bx

	; BtnQuit
	call    AllocWndData
	mov     [bx].TWndData.WndClass, offset ButtonClass
	mov     [bx].TWndData.X, 30
	mov     [bx].TWndData.Y, 150
	mov     [bx].TWndData.SizeX, 120
	mov     [bx].TWndData.SizeY, 20
	mov     si, offset TxtQuit
	lea     di, [bx].TWndData.Text
	call	StrCopy
	mov	si, bx
	mov     di, dx
	call    AddChildWnd
	mov     BtnQuit, bx

	mov	ax, BtnSimplePlay
	mov	ActiveWnd, ax

	pop     di
	pop     si
	pop     dx
	pop     bx
	pop	ax

	ret

MenuStageOnInit endp


; ----------------------------------------------------------
; MenuStageOnCleanUp - MenuStage OnCleanUp Handler
; ----------------------------------------------------------
; Input:
;       si      Pointer to WndData
; Output:       none
; ----------------------------------------------------------
MenuStageOnCleanUp      proc near

	push    si

	mov     si, BtnSimplePlay
	call    DeleteChildWnd
        
	mov     si, BtnNormalPlay
	call    DeleteChildWnd

	mov     si, BtnOption
	call    DeleteChildWnd

	mov     si, BtnRanking
	call    DeleteChildWnd

	mov     si, BtnQuit
	call    DeleteChildWnd

	pop     si

	ret

MenuStageOnCleanUp      endp


; ----------------------------------------------------------
; MenuStageOnDraw - MenuStage OnDraw Handler
; ----------------------------------------------------------
; Input:
;       si      Pointer to WndData
; Output:       none
; ----------------------------------------------------------
MenuStageOnDraw proc near

        push    ax
        mov     ax, 0
        push    ax
        push    ax
        push    [si].TWndData.SizeX
        push    [si].TWndData.SizeY
        mov     ah, 0
        mov     al, [si].TWndData.Color
        push    ax
        call    FillRect
        pop     ax

	call	DefMovingWndOnDraw

        ret

MenuStageOnDraw endp


; ----------------------------------------------------------
; MenuStageOnProcess - MenuStage OnProcess Handler
; ----------------------------------------------------------
; Input:
;       si      Pointer to WndData
; Output:       none
; ----------------------------------------------------------
MenuStageOnProcess      proc near

        ret

MenuStageOnProcess      endp


; ----------------------------------------------------------
; MenuStageOnMouse - MenuStage OnMouse Handler
; ----------------------------------------------------------
; Input:
;       si      Pointer to WndData
; Output:       none
; ----------------------------------------------------------
MenuStageOnMouse        proc near

        call    DefWndOnMouse

        call    DefMovingWndOnMouse

        ret

MenuStageOnMouse        endp


; ----------------------------------------------------------
; MenuStageOnKey - MenuStage OnKey Handler
; ----------------------------------------------------------
; Input:
;       si      Pointer to WndData
; Output:       none
; ----------------------------------------------------------
MenuStageOnKey  proc

	call	DefWndOnKey

        cmp     EventData.EventType, EvtKeyPressed
        jne     SkipStageKeyPressed

        jmp     SkipStageKeyPressed

QKeyPressed:
        mov     Terminated, 1
        inc     [si].TWndData.Color

SkipStageKeyPressed:

        ret

MenuStageOnKey  endp


; ----------------------------------------------------------
; MenuStageOnCommand - MenuStage OnCommand Handler
; ----------------------------------------------------------
; Input:
;       si      Pointer to WndData
; Output:       none
; ----------------------------------------------------------
MenuStageOnCommand      proc near

	push	ax
	push	bx

        mov     bx, CommandData.Sender
        
        cmp     bx, BtnSimplePlay
        jne     SkipBtnSimplePlay

	; Simple Play
	mov	GameMode, GameSimple
        mov     NextStageClass, offset NewStageClass

SkipBtnSimplePlay:
        cmp     bx, BtnNormalPlay
        jne     SkipBtnNormalPlay

	; Normal Play
	mov	GameMode, GameNormal
        mov     NextStageClass, offset NewStageClass

SkipBtnNormalPlay:
        cmp     bx, BtnOption
        jne     SkipBtnOption

	; Option
	mov	DialogTitle, offset TxtDoYouWantTo
	mov	DialogLine1, offset TxtDoYouWantTo
	mov	DialogLine2, offset NullString
	mov	DialogLine3, offset TxtTurnOnMusic
	call	CreateDlgConfirm
	call	ShowModal

	cmp	ModalResult, ModalResultOk
	jne	OptionMusicModalOk

	mov	IsMusicOn, 1
	jmp	DoneOptionMusicModal

OptionMusicModalOk:
	cmp	ModalResult, ModalResultCancel
	jne	OptionMusicModalCancel

	mov	IsMusicOn, 0
	jmp	DoneOptionMusicModal

OptionMusicModalCancel:
DoneOptionMusicModal:

SkipBtnOption:
	cmp	bx, BtnRanking
	jne	SkipBtnRanking

	; Option
	mov	DialogTitle, offset TxtRanking
	mov	DialogLine1, offset TxtTheRecord
	push	si
	mov	si, offset StrNumber
	mov	ax, FastestRecord
	call	NumberToString
	mov	DialogLine2, si
	pop	si
	mov	DialogLine3, offset TxtSeconds

	cmp	FastestRecord, 9999
	jne	ShowRecord

NoRecord:
	mov	DialogLine1, offset NullString
	mov	DialogLine2, offset TxtNoRecord
	mov	DialogLine3, offset NullString

ShowRecord:
	mov	DialogButtons, 2
	call	CreateDlgConfirm
	call	ShowModal
	mov	DialogButtons, 2

SkipBtnRanking:
        cmp     bx, BtnQuit
        jne     SkipBtnQuit

	; Quit
        mov     Terminated, 1
SkipBtnQuit:

	pop	bx
	pop	ax
        ret

MenuStageOnCommand      endp

CodeSeg         ends

                end

